#pragma once

#include "adalog/LogAppender.h"

#include <filesystem>
#include <fstream>
#include <mutex>

namespace adalog
{
    class RollFileAppender : public LogAppender
    {
    public:
        RollFileAppender(const std::filesystem::path& file_path, size_t max_size);
        ~RollFileAppender();
        
        void Append(const char* data, size_t len) override;

    private:
        /**
         * @brief 检查日志文件大小，如果超过最大大小，则进行滚动。若文件为打开，则创建文件。
         */
        void CheckLogFile();
        std::string GetCurrentTimeString();

    private:
        std::filesystem::path file_path_;
        std::filesystem::path last_open_file_path_;
        size_t max_file_size_;
        size_t counter_;
        std::ofstream os_;
        std::mutex mtx_;
    };

} // namespace adalog